//let pako = require('pako');
import pako from 'pako'

export function compression(data: any): string {
    // 数据压缩
    const compressedData = pako.gzip(JSON.stringify(data));
    let arr = Array.from(compressedData);
    let s = "";
    arr.forEach((item, index) => {
        s += String.fromCharCode(item)
    })
    const base64Data = btoa(s);
    return base64Data;
}

/**
 * 
 * @param {string} dataName 
 * @returns {object} 返回解压的数据
 * @des 传入获取sessionStorage中的名字，拿到数据并解压
 */
export function decompress(base64Data: string): object {

    // 将base64编码的数据转换成原始的压缩数据
    const compressedData = new Uint8Array(atob(base64Data).split('').map(char => char.charCodeAt(0)));

    // 对压缩数据进行解压缩处理
    const data = JSON.parse(pako.ungzip(compressedData, { to: 'string' }));
    return data
}